package com.leetcode.June;

import java.util.Stack;

/**
 * @Description: 回文链表
 * @Author: Mr.Fxy
 * @CreateTime: 2024-07-10 10:04
 */
class ListNodes {
    int val;
    ListNodes next;

    ListNodes() {
    }

    ListNodes(int val) {
        this.val = val;
    }

    ListNodes(int val, ListNodes next) {
        this.val = val;
        this.next = next;
    }
}

public class isPalindrome {
    public static boolean isPalindrome(ListNodes head) {
        Stack<Integer> stack = new Stack<>();
        ListNodes cur = head;
        while (cur != null) {
            stack.push(cur.val);
            cur = cur.next;
        }
        while (head != null) {
            if (head.val != stack.pop())
                return false;
            head = head.next;
        }
        return true;
    }
    public static void main(String[] args) {
        ListNodes node1 = new ListNodes(1);
        ListNodes node2 = new ListNodes(2);
        ListNodes node3 = new ListNodes(2);
        ListNodes node4 = new ListNodes(1);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        boolean palindrome = isPalindrome(node1);
        System.out.println(palindrome);

    }
}
